package com.sdry.approval.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sdry.approval.constant.ApprovalTypeEnum;
import com.sdry.approval.entity.PatchCardRequest;
import com.sdry.approval.mapper.LeaveRequestMapper;
import com.sdry.approval.mapper.PatchCardRequestMapper;
import com.sdry.untils.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @program: info_sdry
 * @description: 补卡申请
 * @author: cb
 * @create: 2020-02-21 17:09
 **/
@Service
@Transactional(rollbackFor = Exception.class)
public class PatchCardRequestServiceImpl extends ServiceImpl<PatchCardRequestMapper, PatchCardRequest> implements PatchCardRequestService {

    @Autowired
    private PatchCardRequestMapper patchCardRequestMapper;

    @Autowired
    private LeaveRequestMapper leaveRequestMapper;

    @Autowired
    private MeSponsorService meSponsorService;

    @Override
    public boolean add(PatchCardRequest patchCardRequest, String[] userId) {

        patchCardRequest.setCreateBy(UserContext.getSessionUserIdByServiceImpl().toString());

        Integer effect = patchCardRequestMapper.insert(patchCardRequest);
        if (userId != null){
            for (String str : userId) {
                leaveRequestMapper.addLeaveMan(str,patchCardRequest.getPatchCardId(),"sp","bk");
            }

        }
        if (patchCardRequest.getCopyGiveBy() != null){
            leaveRequestMapper.addLeaveMan(patchCardRequest.getCopyGiveBy().toString(),patchCardRequest.getPatchCardId(),"cs","bk");
        }
        if (effect == 0){
            return false;
        }else {
            //我发起的
            meSponsorService.addMeSponsor(ApprovalTypeEnum.TYPE_APPROVAL_BK.getCode(),patchCardRequest.getPatchCardId());
            return true;
        }
    }
}
